<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<html>
<head>
    <title>_冲正管理</title>
	<script type="text/javascript">
	
	   var dataTables_reverse;
	
		function showDetailFuc(billId) {
			 $.ajax({
				   type : 'post',
					url : '${ctx}/v2/reverse/showDetail',
					data : {
						billId:billId
					},
					dataType : 'json',
					success : function(data) {
					   if(data.status=="success"){
							var itemList = data.itemList;
							var tbody = $("#showItemBody");
							var tbodyStr = "";
							tbody.html(tbodyStr);
							for(var i = 0 ;i < itemList.length ; i++) {
								var item = itemList[i];
								tbodyStr += "<tr>";
								tbodyStr += "<td>"+item.customer.customerName+"</td>";
								tbodyStr += "<td>"+item.warehouse.groupName+"</td>";
								tbodyStr += "<td>"+item.sku+"</td>";
								tbodyStr += "<td>"+item.stockCode+"</td>";
								tbodyStr += "<td>"+item.boxCode+"</td>";
								tbodyStr += "<td>"+item.salesNumFix+"</td>";
								tbodyStr += "<td>"+item.unusedNumFix+"</td>";
								tbodyStr += "<td>"+item.orderNumFix+"</td>";
								tbodyStr += "<td>"+(item.duter==null?"":item.duter.realname)+"</td>";
								tbodyStr += "<td>"+(item.reverser==null?"":item.reverser.realname)+"</td>";
								tbodyStr += "<td>"+item.reversetimeStr+"</td>";
								
								if(item.status == 'undo'){
									tbodyStr += "<td><font color='red'>待冲正</font></td>";
								}else {
									tbodyStr += "<td><font color='blue'>已冲正</font></td>";
								}
								tbodyStr += "</tr>";
							}
							tbody.html(tbodyStr);
					   }else{
							jAlert("系统错误","温馨提示");
					   }
					   
					},
					error : function() {
						jAlert("网络繁忙，请稍后重试...","温馨提示");
						btn.attr("disabled",false);
					}
				 }); 
		}
		
		var selectBillId = "";
	   function assignWarehouse(billId){
		   selectBillId = billId;
			
		    $.ajax({
				   type : 'post',
					url : '${ctx}/v2/reverse/querySubWarehouse',
					data : {
						billId:billId
					},
					dataType : 'json',
					success : function(data) {
					   if(data.status=="success"){
							var warehouseList = data.warehouseList;
							var tbody = $("#selectAssignWarehouseBody");
							var tbodyStr = "";
							tbody.html(tbodyStr);
							for(var i = 0 ;i < warehouseList.length ; i++) {
								var warehouseObj = warehouseList[i];
								tbodyStr += "<tr>";
								tbodyStr += "<td><input type='radio' name='warehouseRadio' value='"+warehouseObj.groupCode+"' ></td>";
								tbodyStr += "<td>"+warehouseObj.groupCode+"</td>";
								tbodyStr += "<td>"+warehouseObj.groupName+"</td>";
								tbodyStr += "</tr>";
							}
							tbody.html(tbodyStr);
					   }else{
							jAlert("系统错误","温馨提示");
					   }
					   
					},
					error : function() {
						jAlert("网络繁忙，请稍后重试...","温馨提示");
						btn.attr("disabled",false);
					}
				 }); 
		   
       }
       function reverseProcess(){
    	   billId = selectBillId;
			var warehouseRadio = $("[name=warehouseRadio]:checked");
			if(!warehouseRadio || !(warehouseRadio.val())) {
				jAlert("请选择仓库","温馨提示");	
			}else {
				jConfirm("冲正单分配到 ("+warehouseRadio.parent().next().next().text()+") 仓库 ,请确定操作!","温馨提醒",function(result){
// 					alert('${ctx}/v2/reverse/toReversePage/'+selectBillId+","+warehouseRadio.val());
					if(result){
						window.location.href = '${ctx}/v2/reverse/toReversePage/'+selectBillId+","+warehouseRadio.val();
					}
				});
			}
       }
       
       $(function() {
           var now = new Date();
		   var today=(now.getFullYear())+"/"+(now.getMonth()+1)+"/"+(now.getDate());    
		   $("#_dataRange").val(today+"-"+today);
		   $("#_dataRange").daterangepicker({format: 'YYYY/MM/DD'});
		   
		   $("#_dataRange_1").daterangepicker({format: 'YYYY/MM/DD'});
		   
		    //初始化dataTables
	        dataTables_reverse=$("#table_reverse").dataTable({
	           "bServerSide":true,
	           "sAjaxSource":'${ctx}/v2/reverse/bindData_reverse',
	           "fnServerData": retrieveData_reverse,
	           "bFilter": false,
	           "bSort": false,
	           "sZeroRecords": "查询无记录！",
	           "bProcessing": true,
	           "oLanguage": {
	               "sUrl": '${ctx}/static/v2/js/plugins/datatables/cn.txt'
	            },
	            "aoColumns": [ 
	                    {"mData": 'customerCode'},
	                    {"mData": 'warehouseCode'},
	                    {
	                      "mData": 'billType',
	                      "render": function (obj) {
	                         var billType=obj;
	                         if(billType=='check'){
	                           return "<font color=blue>盘点冲正</font>";
	                         }else{
	                           return "其它类型";
	                         }
	                      }
	                    },
	                    {"mData": 'billCode'},
	                    {"mData": 'aboutCode'},
	                    {
	                      "mData": 'createTime',
	                      "render": function (obj) {
	                         var time=obj.time;
	                         var date = new Date(time);
	                         return date.Format("yyyy-MM-dd hh:mm:ss");
	                      }
	                    },
	                     {"mData": 'creater.realname'},
	                    {
	                      "mData": 'status',
	                      "render": function (obj) {
	                         var status=obj;
	                         if(status=='undo'){
	                           return "<font color='red'>待冲正</font>";
	                         }else if(status=='doing'){
	                           return "冲正中";
	                         }else if(status=='finish'){
	                           return "<font color='blue'>完成冲正</font>";
	                         }else{
	                           return "";
	                         }
	                      }
	                    },
	                    {
	                     "mData": 'id',
	                      "render": function (obj,type,full) {
	                    		var returnStr = "";
	                    		if(full.customerCode != "MID"||full.isAllocated=='Y'){
	 	                    	  	returnStr += "<a href='${ctx}/v2/reverse/toReversePage/"+full.id+"' class='btn btn-info' >冲正处理</a>";
	                    		}else {
	// 	                    	  	returnStr += "<a href='#showDetail' class='btn btn-info' onclick='showDetailFuc(\""+full.id+"\")' >查看冲正明细</a>";
		                    	  	returnStr += "<a href=\"#showDetail\" class=\"btn btn-primary\"  onclick=\"showDetailFuc('"+full.id+"')\" role=\"button\" data-toggle=\"modal\">查看明细</a>";
		                    	  	returnStr += "<a href=\"#assignSubWarehouseDiv\" class=\"btn btn-primary\"  onclick=\"assignWarehouse('"+full.id+"')\" role=\"button\" data-toggle=\"modal\">冲正处理</a>";
	                      		}
	                         	return returnStr;
	                      }
	                    }
	                    
	             ]
	        });
		 
       });
       
         //回调函数_待处理订单
		function retrieveData_reverse( sSource, aoData, fnCallback ) {
		
		   //查询参数
		   var customerCode=$("#_customerCode").val();
		   aoData.push( { "name": "customerCode", "value": customerCode } );  
		    
		   var warehouseCode=$("#_warehouseCode").val();
		   aoData.push( { "name": "warehouseCode", "value": warehouseCode } );
		   
		   var dataRange=$("#_dataRange").val();
		   aoData.push( { "name": "dataRange", "value": dataRange } );
		   
		   var status=$("#_status").val().trim();
		   aoData.push( { "name": "status", "value": status } );
		   
		   $.ajax({ 
		       "type": "post",
		       "contentType": "application/json", 
		       "url": sSource,    
		       "dataType": "json",   
		       "data": JSON.stringify(aoData),
		       "success": function(resp) {  
		       			fnCallback(resp);
		       		}
		   });    
		}
       
	   
	    //查询
		function query(){
		   dataTables_reverse.fnDraw();
		}
	  
	  
	  //打开创建计划窗体
	  function openWindow(){
		   var now = new Date();
		   var today=(now.getFullYear())+"/"+(now.getMonth()+1)+"/"+(now.getDate());    
		   $("#_dataRange_1").val(today+"-"+today);
		   $("#_dataRange_1").daterangepicker({format: 'YYYY/MM/DD'});
		  
		  $("#planType").val("");
		  $("#customerCode_1").val("");
		  $("#warehouseCode_1").val("");
		  $("#customerCode_2").val("");
		  $("#warehouseCode_2").val("");
		  
		  $("#changeTable").css("display","none");
          $("#skuTable").css("display","none");
          $("#stockTable").css("display","none");
          $("#customerTable").css("display","none");
          
          $("#grid_1").html('');
          $("#grid_2").html('');
          $("#grid_3").html('');
	  }
	  
    </script>
</head>

<body>
<section class="content">

    <!-- 查询条件  start-->
   	<div class="row">
	<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary" >
			    
				<div class="box-header">
					<i class="fa fa-search"></i>
					<h3 class="box-title">搜索栏</h3>
				</div>
				
				<div class="box-body">
				    
				    <!-- 第一行 -->
					<div class="form-group row">
					
						<div class="col-sm-3">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> 商家名称： </span> 
								<select class="form-control required" id="_customerCode" onchange="ajaxAreaGroup(this,'_warehouseCode')">
									<option value=""></option>
									<c:forEach items="${customerList}" var="customer">
										<option value="${customer.customerCode}"
											<c:if test="${customerCode eq customer.customerCode}">selected=selected</c:if>>${customer.customerName}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						
						<div class="col-sm-3">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> 作业仓库： </span> 
								<select class="form-control span3" id="_warehouseCode" >
									<option value=""></option>
									<c:forEach items="${warehouseList}" var="warehouse">
										<option value="${warehouse.groupCode}" >${warehouse.groupName}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						
						<div class="col-sm-3">
							<div class="input-group col-sm-12">
								<span class="input-group-text">当前状态： </span> 
								<select  name="_status"  id="_status" class="form-control span3">
									<option value=""    <c:if test="${status eq ''}">selected=selected</c:if> ></option>
								    <option value="undo"   <c:if test="${status eq 'undo'}">selected=selected</c:if> >待冲正</option>
								    <option value="doing"   <c:if test="${status eq 'doing'}">selected=selected</c:if> >冲正中</option>
								    <option value="finish"   <c:if test="${status eq 'finish'}">selected=selected</c:if> >完成冲正</option>
								</select>
							</div>
						</div>
						
					</div>

                    <!-- 第二行 -->
					<div class=" form-group row">

						<div class="col-sm-6">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> 创建日期： </span>

								<div class="input-group">
									<div class="input-group-addon">
										<i class="fa fa-calendar"></i>
									</div>
									<input type="text" class="form-control pull-right" id="_dataRange" name="_dataRange" value="${param._fromDate}"/>
								</div>
								
							</div>
						</div>
						
						<div class="col-sm-6">
							<button class="btn btn-primary" type="button" onclick="query();">搜索</button>
							<a href="#" role="button" data-toggle="modal"  class="btn btn-primary"  >发起冲正</a>
						</div>
						
					</div>
					
				</div>

			</div>

	 </div>
	</div>
    <!-- 查询条件 end -->
    
    <!-- 查询结果 start -->
	<div class="form-group row" >
	<div class="col-md-12">
	
       <div class="box box-primary">
			 <div class="box-header"></div>
			 <div class="box-body table-responsive">
			     <table id="table_reverse" class="table table-bordered table-striped">
			         <thead>
			             <tr>
			                <th style="width: 80px;">商家名称</th>
						    <th style="width: 80px;">作业仓库</th>
						    <th style="width: 80px;">冲正类型</th>
						    <th >冲正单号</th>
						    <th >相关单号</th>
							<th >创建时间</th>
							<th style="width: 100px;">创建人</th>
							<th style="width: 100px;">当前状态</th>
							<th style="width: 80px;">操作</th>
			             </tr>
			         </thead>
			     </table>
			 </div>
		 </div>
	
	</div>
	</div>
	
	<!-- 创建盘点计划 -------------------------------------------------------------------------------------------------->
	<div class="modal fade" id="detail">
	<div class="modal-dialog" style="width: 60%;">
	<div class="modal-content">
	
		<!-- 体 -->
		<div class="modal-body form-search" style="height:400px;" >
		
		   
		   <div class="box box-solid box-primary">
                <div class="box-header">
                     <span class="box-title">选择盘点类型</span>
					  <select id="planType" name="planType"   onchange="planTypeChange();" style="margin-top: 8px;margin-left: 10px;">
							<option value="" selected="selected"></option>
							<option value="change">动碰盘点</option>
							<option value="sku">按商品盘点</option>
							<option value="stock">按库位盘点</option>
							<option value="customer">按商家盘点</option>
					  </select>
                </div>
			</div>   
		    
		    <!-- 动碰盘点 -->
		    <div style="height: 330px; overflow:auto;" id="changeTable" style="display:none; "> 
			<table class="table table-striped table-bordered table-condensed" >
			    <tr>
			      <td colspan="5">
			        <label>商家:</label>
					<select id="customerCode_1" name="customerCode_1"  onchange="ajaxAreaGroup(this,'warehouseCode_1');">
					        <option value=""></option>
							<c:forEach items="${customerList}" var="customer">
								<option value="${customer.customerCode}" >${customer.customerName}</option>
							</c:forEach>
					</select> 
		   
				   <label style="margin-left: 20px;">仓库:</label>
				   <select  name="warehouseCode_1"  id="warehouseCode_1" >
						<option value=""></option>
						<c:forEach items="${warehouseList}" var="w">
						   <option value="${w.groupCode}" >${w.groupName}</option>
					   </c:forEach>
					</select>
			        
				 	<label style="margin-left: 20px;">时间:</label> 
				 	<input type="text" id="_dataRange_1" name="_dataRange_1" style="width: 200px;"/>
				 	
				    <button type="button" onclick="query_change();"  class="btn btn-primary" style="margin-left: 20px;">查询</button>
			      </td>
			    </tr>
			    
				<tr>
				    <th>序号</th>
				    <th>SKU编码</th>
				    <th>入库数量</th>
				    <th >出库数量</th>
				    <th >变动数量</th>
				</tr>
				<tbody id="grid_1" ></tbody>
			  </table>
			  </div>
		     
		    <!-- 按sku -->
		    <div style="height: 330px; overflow:auto; display:none;" id="skuTable" > 
			<table class="table table-striped table-bordered table-condensed" >
			    <tr>
			      <td colspan="5">
			      <label>商家:</label>
				  <select id="customerCode_2" name="customerCode_2"  onchange="ajaxAreaGroup(this,'warehouseCode_2');">
				          <option value=""></option>
				  		  <c:forEach items="${customerList}" var="customer">
								<option value="${customer.customerCode}" >${customer.customerName}</option>
						  </c:forEach>
				   </select> 
		   
				   <label style="margin-left: 20px;">仓库:</label>
				   <select  name="warehouseCode_2"  id="warehouseCode_2" >
						<option value=""></option>
						<c:forEach items="${warehouseList}" var="w">
						   <option value="${w.groupCode}" >${w.groupName}</option>
					   </c:forEach>
					</select>
			        
				 	<label>SKU编码：</label>
				 	<input type="text"  id="sku" > 
				 	
				    <button type="button btn-primary" onclick="queryGoods();"   class="btn btn-primary">搜索</button>
				    <button type="button" onclick="querySelected();" class="btn btn-primary">已选择</button>
				    
			      </td>
			    </tr>
			    
				<tr>
				    <th >商家</th>
				    <th >仓库</th>
				    <th >SKU</th>
				    <th >现库存量</th>
				    <th style="width:40px; text-align: center;">
				      <input type="checkbox" name="var_allCheck_sku" id="var_allCheck_sku"  onclick="allCheck_sku();" />
				    </th>
				</tr>
				
				<tbody id="grid_2"></tbody>
			 </table>
			 </div>
			 
			<!-- 按库位 -->
			<div style="height: 330px; overflow:auto; display:none;" id="stockTable" > 
			<table  class="table table-striped table-bordered table-condensed" >
			    <tr>
			      <td colspan="5">
			      <label>商家:</label>
				  <select id="customerCode_3" name="customerCode_3"  onchange="ajaxAreaGroup(this,'warehouseCode_3');">
				          <option value=""></option>
				  		  <c:forEach items="${customerList}" var="customer">
								<option value="${customer.customerCode}" >${customer.customerName}</option>
						  </c:forEach>
				   </select> 
		   
				   <label style="margin-left: 20px;">仓库:</label>
				   <select  name="warehouseCode_3"  id="warehouseCode_3" >
						<option value=""></option>
						<c:forEach items="${warehouseList}" var="w">
						   <option value="${w.groupCode}" >${w.groupName}</option>
					   </c:forEach>
					</select>
			        
				 	<label>stock编码：</label>
				 	<input type="text"  id="stock" > 
				    <button type="button" onclick="queryStock();"   class="btn btn-primary">搜索</button>
				    <button type="button" onclick="querySelected_stock();" class="btn btn-primary">已选择</button>
			      </td>
			    </tr>
			    
				<tr>
				    <th >商家</th>
				    <th >仓库</th>
				    <th >库位</th>
				    <th style="width:40px; text-align: center;">
				      <input type="checkbox" name="var_allCheck_stock" id="var_allCheck_stock" onclick="allCheck_stock()" />
				    </th>
				</tr>
				
				<tbody id="grid_3">
				</tbody>
			 </table>
			 </div>
			 
			<!-- 按商家 -->
			<div style="height: 330px; overflow:auto; display:none;" id="customerTable"  > 
			<table   class="table table-striped table-bordered table-condensed" >
			   <tr>
			      <td colspan="5">
				      <label>商家:</label>
					  <select id="customerCode_4" name="customerCode_4"  onchange="ajaxAreaGroup(this,'warehouseCode_4');">
					          <option value=""></option>
					  		  <c:forEach items="${customerList}" var="customer">
									<option value="${customer.customerCode}" >${customer.customerName}</option>
							  </c:forEach>
					   </select> 
			   
					   <label style="margin-left: 20px;">仓库:</label>
					   <select  name="warehouseCode_4"  id="warehouseCode_4" >
							<option value=""></option>
							<c:forEach items="${warehouseList}" var="w">
							   <option value="${w.groupCode}" >${w.groupName}</option>
						   </c:forEach>
						</select>
			        
			      </td>
			    </tr>
			 </table>
			 </div>
			 
			  
		</div>
		
		<!-- 尾 -->
		<div class="modal-footer">
			<input id="submit_btn" class="btn btn-primary"  type="button" value="创建计划" onclick="mySubmit();"/>&nbsp;	
			<input id="cancel_btn" class="btn" type="button" data-dismiss="modal" value="返回" />
		</div>
		
		
	  </div>
	  </div>	
     </div>
     <!------------------------------------------------------------------------------------------------------------->
     
     
     <div class="modal fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" id="showDetail">
			<div class="modal-dialog" style="width:1400px;" >
			<div class="modal-content" >
			<div class="modal-header">
				<h3 id="myModalLabel">
					冲正明细
				</h3>
			</div>
			<div class="modal-body" style="width: 1300;height:300px; overflow: auto;">
				<table class="table table-striped table-bordered table-condensed">
					<thead>
						<tr>
							<th>商家</th>
							<th>仓库</th>
							<th>sku</th>
							<th>库位</th>
							<th>箱位</th>
							<th>可销售（校对）</th>
							<th>不可销售（校对）</th>
							<th>订单占用（校对）</th>
							<th>责任人</th>
							<th>冲正人</th>
							<th>冲正时间</th>
							<th>状态</th>
						</tr>
					</thead>
					<tbody id="showItemBody">
						
					</tbody>
				</table>
			</div>
			<div class="modal-footer">
				<button class="btn" data-dismiss="modal" aria-hidden="true">
					关闭
				</button>
			</div>
		</div>
		</div>
	</div>
	
	
     <div class="modal fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" style="width:1400;" id="assignSubWarehouseDiv">
			<div class="modal-dialog" >
			<div class="modal-content" >
			<div class="modal-header">
				<h3 id="myModalLabel">
					分配仓库
				</h3>
			</div>
			<div class="modal-body" style="width: 1300;height:300px; overflow: auto;">
				<table class="table table-striped table-bordered table-condensed"
					id="subWarehouseList">
					<thead>
						<tr>
							<th></th>
							<th>仓库编码</th>
							<th>仓库名称</th>
						</tr>
					</thead>
					<tbody id="selectAssignWarehouseBody">
						
					</tbody>
				</table>
			</div>
			<div class="modal-footer">
				<a href="#" onclick="reverseProcess()" role="button"  class="btn btn-primary" data-toggle="modal">
					确认分配处理
				</a>
				<button class="btn" data-dismiss="modal" aria-hidden="true">
					关闭
				</button>
			</div>
		</div>
		</div>
	</div>
     
     
</section>     
</body>
</html>
